package com.king.pattem.future;

/**
 * @author taomk
 * @version 1.0
 * @since 15-1-30 下午10:01
 */
public class FutureData implements Data {

	// FutureData是RealData的包装
	protected RealData realData = null;

	protected boolean isReady = false;

	public synchronized void setRealData(RealData realData) {
		if (isReady) {
			return;
		}
		this.isReady = true;
		this.realData = realData;
		// RealData已经被注入，通知getResult()
		notifyAll();
	}

	public synchronized String getResult() {
		try {
			while (!isReady) {
				// 一直等待，直到RealData被注入
				wait();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 由RealData实现
		return realData.result;
	}
}
